/**
 * 全景相册服务
 */
define(function(require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require("util");
    var _ = require("underscore");
    var angular = require("angular");

    var panoramaModule = angular.module('panoramaModule', []);

    function panoramaSerivce($http, $q) {
        var service = {
            pageIndex: 1,

            query: function (pageIndex) {
                var filters = {};
                filters.pageIndex = pageIndex || 1;
                filters.pageSize = service.pageSize;
                var url = '/api/index.php?r=tools/basis-function-plugin/get-pa-panorama-list';

                if (arguments.callee.canceller) {
                    // 如果查询时，前面的查询还没完成，把它中止
                    arguments.callee.canceller.resolve();
                }
                arguments.callee.canceller = $q.defer();

                return $http.get(url, { params: filters, timeout: arguments.callee.canceller.promise })
                    .success(function (data) {
                        service.pageIndex = filters.pageIndex;
                        service.total = data.data.total;
                        service.data = data.data.list;
                    });
            },

            // 删除
            remove: function (record) {
                var data = {
                    pa_panoramaId: record.pa_panoramaId
                };

                var url = '/api/index.php?r=tools/basis-function-plugin/del-pa-panorama';
                return $http.post(url, data).then(function () {
                    service.query();
                });
            },

            convertRecord: function (record) {
                if (!record) {
                    return {
                        type: '0',
                        taxis: 0,
                        frontpic: {
                            "name": "tj_01_o_0.jpg",
                            "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-5c1b-279e-bf10-251e676e96b2.jpg",
                            "preview": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-5d05-a3ad-c758-ae2783d34629.jpg",
                            "thumblist": [
                              {
                                  "width": 100,
                                  "height": 100,
                                  "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-5d05-a3ad-c758-ae2783d34629.jpg"
                              }
                            ]
                        },
                        rightpic: {
                            "name": "tj_01_o_1.jpg",
                            "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-7336-a780-c5a1-419823799f39.jpg",
                            "preview": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-73e8-f731-1868-59804834cf31.jpg",
                            "thumblist": [
                              {
                                  "width": 100,
                                  "height": 100,
                                  "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-73e8-f731-1868-59804834cf31.jpg"
                              }
                            ]
                        },
                        backpic: {
                            "name": "tj_01_o_2.jpg",
                            "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-8772-836d-67de-352a243d40da.jpg",
                            "preview": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-897f-568e-a605-7431d3bf1436.jpg",
                            "thumblist": [
                              {
                                  "width": 100,
                                  "height": 100,
                                  "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-897f-568e-a605-7431d3bf1436.jpg"
                              }
                            ]
                        },
                        leftpic: {
                            "name": "tj_01_o_3.jpg",
                            "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051914%2Ffile_39d07d25-a4e6-d077-31ff-87f1340f3c73.jpg",
                            "preview": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-a5aa-7640-971c-87024fb1b9d1.jpg",
                            "thumblist": [
                              {
                                  "width": 100,
                                  "height": 100,
                                  "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-a5aa-7640-971c-87024fb1b9d1.jpg"
                              }
                            ]
                        },
                        toppic: {
                            "name": "tj_01_o_4.jpg",
                            "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-b56d-535d-9bff-bab3425b9012.jpg",
                            "preview": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-b5f8-2ed8-b5fb-351e1aa087be.jpg",
                            "thumblist": [
                              {
                                  "width": 100,
                                  "height": 100,
                                  "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-b5f8-2ed8-b5fb-351e1aa087be.jpg"
                              }
                            ]
                        },
                        bottompic: {
                            "name": "tj_01_o_5.jpg",
                            "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-c78f-45f1-1f82-6eb01431fc58.jpg",
                            "preview": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-c81b-ff2e-ddc4-90350a7cc9b1.jpg",
                            "thumblist": [
                              {
                                  "width": 100,
                                  "height": 100,
                                  "url": "http://ci-yunketest.oss-cn-shenzhen.aliyuncs.com/weiloushu%2F201505%2F2015051915%2Ffile_39d07d25-c81b-ff2e-ddc4-90350a7cc9b1.jpg"
                              }
                            ]
                        }
                    };
                }

                var fields = ['frontpic', 'rightpic', 'backpic', 'leftpic', 'toppic', 'bottompic'];
                _.each(fields, function (field) {
                    var fieldData = JSON.parse(record[field]);
                    record[field] = fieldData[0];
                });

                if (record.resource_file) {
                    record.resource_file = JSON.parse(record.resource_file)[0];
                }

                return record;
            },

            save: function (record) {
                var formData = angular.copy(record);
                var fields = ['frontpic', 'rightpic', 'backpic', 'leftpic', 'toppic', 'bottompic'];
                _.each(fields, function (field) {
                    delete formData[field].state;
                    formData[field] = JSON.stringify([formData[field]]);
                });

                if (formData.resource_file) {
                    delete formData.resource_file.state;
                    formData.resource_file = JSON.stringify([formData.resource_file]);
                }

                return $http.post('/api/index.php?r=tools/basis-function-plugin/write-pa-panorama', formData)
                    .success(function() {
                        service.query();
                    });
            }
        };

        return service;
    }
    panoramaModule.factory('panorama', ['$http', '$q', panoramaSerivce]);
    
    module.exports = panoramaModule;
});